package utils;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import calendar_ex.Appointment;
import calendar_ex.CalendarDate;
import calendar_ex.CalendarEx;
import calendar_ex.CalendarTime;

public class AgendaDay extends JPanel {
	
	private CalendarDate date;
	private boolean empty=true;
	public boolean isEmpty(){
		return this.empty;
	}
	/*
	 * nextDay - Input Date, return the Date of the next day.
	 */

	public AgendaDay(CalendarDate d){
		date=d;
		this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
		this.setAlignmentX(LEFT_ALIGNMENT);
		this.setAlignmentY(TOP_ALIGNMENT);
		this.setBackground(Color.white);
		JPanel datePanel = new JPanel();
		datePanel.setBackground(Color.white);
		datePanel.setLayout(new BoxLayout(datePanel,BoxLayout.Y_AXIS));
		int day = date.day;
		int month = date.month;
		int year = date.year;
		// DayName label
		
		Calendar calendar=Calendar.getInstance();
		calendar.set(year, month, day);
		JLabel dayLabel,dayNumber;
		dayLabel = new JLabel();
		dayLabel.setBackground(Color.white);
		dayNumber = new JLabel();
		dayNumber.setBackground(Color.white);
		
		SimpleDateFormat format = new SimpleDateFormat("EEE");
		dayLabel.setText(format.format(calendar.getTime()));
		format = new SimpleDateFormat("d");
		String dayNo = format.format(calendar.getTime());
		String f = "0";
		if(Integer.parseInt(dayNo)>9)
			f="";
		dayNumber.setText(f+dayNo);
		
		// DayNumber Label
		
		// Add to panel
		dayLabel.setFont(new Font("Arial", Font.BOLD, 16));
		datePanel.add(dayLabel);
		dayNumber.setFont(new Font("Arial", Font.BOLD, 30));
		datePanel.add(dayNumber);
		
		JLabel padLabel = new JLabel("   ");
		padLabel.setBackground(Color.white);
		// Day Agenda Contents
		JPanel eventPanel = new JPanel();
		
		eventPanel.setLayout(new BoxLayout(eventPanel,BoxLayout.Y_AXIS));
		eventPanel.setBackground(Color.white);
		
		CalendarEx calData = new CalendarEx();
        calData.openCalendar("calendar.txt");
        
		List<Appointment> daysAppointments;
		daysAppointments = calData.getAppointmentsBetweenDates(date,date);
		Iterator itApp = daysAppointments.iterator();
		int count=0;
		while(itApp.hasNext()){
			this.empty=false;
			count++;
			Appointment temp  =  (Appointment) itApp.next();
			CalendarTime startTime = temp.getStart_time();
			CalendarTime endTime = temp.getEnd_time();
			int appointmentDuration = temp.getDuration();
			String description = temp.description;
			JLabel startTimeLabel = new JLabel();
			String fr= "0";
			if(startTime.hr>9)
				fr = "";
			startTimeLabel.setText(fr+startTime.hr+" ");
			
			startTimeLabel.setPreferredSize(new Dimension(10,10));
			JLabel descriptionLabel = new JLabel(description);
			descriptionLabel.setPreferredSize(new Dimension(10,10));
			JPanel textPanel = new JPanel();
			textPanel.setLayout(new BoxLayout(textPanel,BoxLayout.X_AXIS));
			textPanel.setAlignmentX(LEFT_ALIGNMENT);
			textPanel.setBackground(Color.WHITE);
			textPanel.add(startTimeLabel);
			textPanel.add(descriptionLabel);
			eventPanel.add(textPanel);
		}
		if(count>0){
			add(datePanel);
			add(padLabel);
			add(eventPanel);
		}
	}
}
